package com.ycp.erp.warehouse.ui.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jakewharton.rxbinding2.view.RxView;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.utils.KeyboardUtil;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.multitytype.MultiTypeAdapter;
import com.one.common.view.widget.MyTitleBar;
import com.one.common.view.widget.ScrollLinearLayoutManager;
import com.ycp.erp.R;
import com.ycp.erp.warehouse.model.bean.ProcurementWarehouse;
import com.ycp.erp.warehouse.model.extra.WarehouseExtra;
import com.ycp.erp.warehouse.ui.binder.ConfirmPWarehouseBinder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * Class Des:确认采购入库
 * Created by bjh on 2021/8/31.
 */
@Route(path = RouterPath.CONFIRM_PROCURE_WAREHOUSE)
public class ConfirmPWarehouseActivity extends BaseActivity {
    @BindView(R.id.tvSelectWareHouse)
    TextView tvSelectWareHouse;
    @BindView(R.id.rlSelectWareHouse)
    RelativeLayout rlSelectWareHouse;
    @BindView(R.id.tvSelectSupplier)
    TextView tvSelectSupplier;
    @BindView(R.id.rlSelectSupplier)
    RelativeLayout rlSelectSupplier;
    @BindView(R.id.tvSelectPDate)
    TextView tvSelectPDate;
    @BindView(R.id.rlSelectPDate)
    RelativeLayout rlSelectPDate;
    @BindView(R.id.tvSelectPTime)
    TextView tvSelectPTime;
    @BindView(R.id.rlSelectPTime)
    RelativeLayout rlSelectPTime;
    @BindView(R.id.tvTotalPrice)
    TextView tvTotalPrice;
    @BindView(R.id.rlTotalPrice)
    RelativeLayout rlTotalPrice;
    @BindView(R.id.tvPriceHint2)
    TextView tvPriceHint2;
    @BindView(R.id.etDiscountPrice)
    EditText etDiscountPrice;
    @BindView(R.id.rlDiscountPrice)
    RelativeLayout rlDiscountPrice;
    @BindView(R.id.tvTotalPricePayableHint2)
    TextView tvTotalPricePayableHint2;
    @BindView(R.id.eTotalPricePayable)
    EditText eTotalPricePayable;
    @BindView(R.id.rlTotalPricePayable)
    RelativeLayout rlTotalPricePayable;
    @BindView(R.id.swCredit)
    Switch swCredit;
    @BindView(R.id.rlCredit)
    RelativeLayout rlCredit;
    @BindView(R.id.tvAddRemarks)
    TextView tvAddRemarks;
    @BindView(R.id.tvRemarks)
    TextView tvRemarks;
    @BindView(R.id.rcv)
    RecyclerView rcv;
    @BindView(R.id.nsc)
    NestedScrollView nsc;
    @BindView(R.id.tvSubmit)
    TextView tvSubmit;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    private MultiTypeAdapter adapter;
    private ArrayList<String> firstTimeData;
    private ArrayList<String> SecondTimeData;
    private ArrayList<String> thirdTimeData;

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("确认采购入库商品").hideLine();
    }
    @SuppressLint("CheckResult")
    @Override
    public void initData() {
        super.initData();
        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(getContext());
        scrollLinearLayoutManager.setScrollEnable(false);
        rcv.setLayoutManager(scrollLinearLayoutManager);
        adapter = new MultiTypeAdapter();
        ConfirmPWarehouseBinder mBinder = new ConfirmPWarehouseBinder();
        adapter.register(ProcurementWarehouse.class, mBinder);
        rcv.setAdapter(adapter);
        adapter.setItems(getData());
        adapter.notifyDataSetChanged();

        RxView.clicks(rlSelectPDate).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            TimePickerView pvTime = new TimePickerBuilder(myRxActivity, (date, v) -> tvSelectPDate.setText(getTime(date)))
                    .setSubmitColor(getResources().getColor(R.color.root_green))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            pvTime.show();
        });
        initFirstTimeData();
        initSecondTimeData();
        initThirdTimeData();
        RxView.clicks(rlSelectPTime).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            //筛选dialog
            OptionsPickerView<String> build = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> tvSelectPTime.setText(String.format("%s:%s", SecondTimeData.get(options2), thirdTimeData.get(options3)))).setSubmitColor(getResources().getColor(R.color.root_green))
                    .setCancelColor(getResources().getColor(R.color.gray_d6))
                    .setLineSpacingMultiplier(2.8f)
                    .build();
            build.setNPicker(firstTimeData,SecondTimeData,thirdTimeData);
            build.show();
        });
        RxView.clicks(tvSubmit).throttleFirst(1, TimeUnit.SECONDS).subscribe(o -> {
            new KeyboardUtil(mContext).hideSoftInputFromWindow(myRxActivity);
            RouterManager.getInstance().go(RouterPath.WAREHOUSE_SUCCESS,new WarehouseExtra("入库成功","入库成功"));

        });
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    /**
     * 模拟数据
     *
     * @return
     */
    private ArrayList<ProcurementWarehouse> getData() {
        ArrayList<ProcurementWarehouse> procurementWarehouses = new ArrayList<>();
        ProcurementWarehouse procurementWarehouse = new ProcurementWarehouse();
        procurementWarehouse.setGoodsName("测试");
        procurementWarehouse.setGoodsDesc("测试描述");
        procurementWarehouse.setGoodsNum(0);
        procurementWarehouse.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse);
        ProcurementWarehouse procurementWarehouse1 = new ProcurementWarehouse();
        procurementWarehouse1.setGoodsName("测试");
        procurementWarehouse1.setGoodsDesc("测试描述");
        procurementWarehouse1.setGoodsNum(0);
        procurementWarehouse1.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse1);
        ProcurementWarehouse procurementWarehouse2 = new ProcurementWarehouse();
        procurementWarehouse2.setGoodsName("测试");
        procurementWarehouse2.setGoodsDesc("测试描述");
        procurementWarehouse2.setGoodsNum(0);
        procurementWarehouse2.setGoodsPrice("100");
        procurementWarehouses.add(procurementWarehouse2);
        return procurementWarehouses;
    }
    private void  initFirstTimeData(){
        firstTimeData = new ArrayList<>();
        firstTimeData.add("上午");
        firstTimeData.add("下午");
    }
    private void  initSecondTimeData(){
        SecondTimeData = new ArrayList<>();
        for(int i=1;i<=24;i++){
            SecondTimeData.add(""+i);
        }
    }
    private void initThirdTimeData() {
        thirdTimeData = new ArrayList<>();
        for (int i = 0; i <= 59; i++) {
            if (i < 10)
                thirdTimeData.add("0" + i);
            else {
                thirdTimeData.add("" + i);
            }
        }
    }
    @Override
    public int getLayoutResId() {
        return R.layout.activity_confirm_p_warehouse;
    }
}
